package com.yanqu.road.dataHelper.manager;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DBManager;

import java.io.File;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DbMgr extends BaseMgr {

    private static ArrayList<String> connListName;

    public static boolean initDB(String dirPath) {
        String path = dirPath + "config" + File.separator;
        DBManager.initConfig(path + "dbconfig.properties");

//        DbMgr dbMgr = new DbMgr();
//        dbMgr.testServerDb();
//        dbMgr.testConfigDb();

        return true;
    }

    public boolean testServerDb() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select * from t_s_server_list limit 1");
            rs = call.executeQuery();
            while (rs.next()) {
                long serverId = rs.getLong("serverId");
                System.out.println("serverId : " + serverId);
            }
        } catch (SQLException e) {
            logger.error("异常错误",e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return true;
    }

    public boolean testConfigDb() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select * from t_p_title limit 1");
            rs = call.executeQuery();
            while (rs.next()) {
                long level = rs.getLong("level");
                System.out.println("level : " + level);
            }
        } catch (SQLException e) {
            logger.error("异常错误",e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return true;
    }


}
